﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.RolesCommand;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.RolesCommandHandler
{
    public class RoleDelCommandHandler : IRequestHandler<RoleDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> userrReppos;
        private readonly IMapper mapper;

        public RoleDelCommandHandler(IBaseRepository<Role> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }


        public Task<APIResult<object>> Handle(RoleDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var obj = userrReppos.GetAll().Result.FirstOrDefault(x => x.RoleId == request.RoleId);
            obj.IsDeleted = true;
            var res = userrReppos.Update(obj).Result;
            if (res == 1)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
